package com.wnxy.sddfp.bank.controller;

import com.wnxy.sddfp.bank.mq.MessageProducer;
import com.wnxy.sddfp.bank.service.ITradeLogService;
import com.wnxy.sddfp.common.model.dto.bank.BankCardAddDTO;
import com.wnxy.sddfp.common.model.dto.bank.BankCardRequestDto;
import com.wnxy.sddfp.common.model.dto.bank.SignatureDTO;
import com.wnxy.sddfp.common.model.dto.bid.BankBidDTO;
import com.wnxy.sddfp.common.model.dto.bid.BidBalanceDTO;
import com.wnxy.sddfp.common.model.vo.trade.BorrowSyncRecordVo;
import com.wnxy.sddfp.common.result.Result;
import com.wnxy.sddfp.common.util.BaseResult;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 托管系统交易记录表 前端控制器
 * </p>
 */
@RestController
public class TradeLogController {

    @Autowired
    private ITradeLogService tradeLogService;
//    @Autowired
//    private MessageProducer messageProducer;

    /**
     * 绑卡生成交易记录
     * http://localhost:5500/bank/access/card
     */
    @PostMapping("/bank/access/card")
    public SignatureDTO proxyBindCardLog(@RequestBody BankCardAddDTO bankCardAddDTO) {
        SignatureDTO signatureDto = tradeLogService.proxyBindCardLog(bankCardAddDTO);
        return signatureDto;
    }

    /**
     * 银行托管系统绑卡回调结果
     * 回调地址：http://localhost:5500/bank/access/receive
     */
    @ApiOperation("银行托管系统绑卡回调结果")
    @PostMapping(value = "/bank/access/receive")
    public String callbackUpdate(@RequestBody BankCardRequestDto bankCardRequestDto){
        //1. 修改托管系统交易状态为已同步
        tradeLogService.modifyRequestStatus(bankCardRequestDto.getReqCode(),1);
        //2.发送消息给用户中心 （更新用户信息-绑卡状态）（银行卡编码、开户行名称）
        //messageProducer.sendMsg(bankCardRequestDto);
        //3.返回结果
        return "OK";
    }

    /**
     * 借款记录审核，同步借款数据到银行托管系统
     */
    @ApiOperation("借款记录审核，同步数据")
    @PostMapping("/bank/access/sync/borrow")
    public Result syncBorrowRecord(@RequestBody BorrowSyncRecordVo borrowSyncRecordVo) {
        tradeLogService.syncBorrowRecord(borrowSyncRecordVo);
        return Result.ok();
    }


    @ApiOperation("获取机构账户余额信息")
    @GetMapping("/bank/access/bid/balance/{userCode}")
    public Result<BidBalanceDTO> getBidBalance(@PathVariable("userCode") String userCode) {
        BidBalanceDTO balanceDetailsDTO = tradeLogService.getBidBalance(userCode);
        return Result.ok(balanceDetailsDTO);
    }

    @ApiOperation("同步机构认购记录到银行托管")
    @PostMapping("/bank/access/bid/add")
    public Result<String> bankBidService(@RequestBody BankBidDTO bankBidDTO) {
        BaseResult baseResult =
                tradeLogService.bankBidService(bankBidDTO);

        Result<String> result=new Result<>();
        result.setCode(200);
        result.setMessage(baseResult.getRespMsg());
        return result;
    }
}
